package com.zxl.tsc.common;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SessionInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		/*// 不过滤的uri
		String[] notFilter = new String[] { "/cloud_note/login/showloginpage", "/cloud_note/login/showloginpage2", "/cloud_note/login/showloginpage3",
				"/cloud_note/login/loginnow", "/cloud_note/user/regUser" };

		// 请求的uri
		String uri = request.getRequestURI();
		// uri中包含background时才进行过滤
		// 是否过滤
		for (String s : notFilter) {
			if (uri.indexOf(s) != -1) {
				return true;
			}
		}*/
		HttpSession session = request.getSession();
		if (session.getAttribute("userinfo") == null) {
			// response.sendRedirect(request.getContextPath()+"/sys/login");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			// JS代码
			out.write("<script type='text/javascript'>");
			out.write("alert('您还没有登录,请登录后访问');");
			// 跳页面
			out.write("window.top.location.href='" + request.getContextPath() + "/login/showloginpage2';");
			out.write("</script>");
			out.flush();
			out.close();

			return false;
		} else {
			return true;
		}

	}

}